<?php

namespace app\controllers\h5\my;

use app\controllers\h5\base\H5Controller;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserConsume;

class AccountController extends H5Controller
{
    public function actionAccount()
    {
        $account = UserAccount::find()
            ->where(['user_id' => $this->user->id])
            ->asArray()
            ->one();

        return $this->successEncode(['account' => $account]);
    }

    public function actionCommission()
    {
        // $type = $this->post('type');

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = UserCommission::find()
            ->where(['status' => 1])
            ->andWhere(['>', 'balance', 0])
            ->andWhere(['user_id' => $this->user->id]);

        // switch ($type) {
        //     case 'score':
        //         $query->andWhere(['>', 'score', 0]);
        //         break;
        //     case 'ticket':
        //         $query->andWhere(['>', 'ticket', 0]);
        //         break;
        //     case 'platform_ticket':
        //         $query->andWhere(['>', 'platform_ticket', 0]);
        //         break;
        //     case 'balance':
        //         $query->andWhere(['>', 'balance', 0]);
        //         break;
        // }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }
    public function actionCommission2()
    {
        // $type = $this->post('type');

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = UserCommission::find()
            ->where(['status' => 1])
            ->andWhere(['>', 'platform_ticket', 0])
            ->andWhere(['user_id' => $this->user->id]);

        // switch ($type) {
        //     case 'score':
        //         $query->andWhere(['>', 'score', 0]);
        //         break;
        //     case 'ticket':
        //         $query->andWhere(['>', 'ticket', 0]);
        //         break;
        //     case 'platform_ticket':
        //         $query->andWhere(['>', 'platform_ticket', 0]);
        //         break;
        //     case 'balance':
        //         $query->andWhere(['>', 'balance', 0]);
        //         break;
        // }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        // var_dump($list);exit;

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    public function actionConsume()
    {
        // $type = $this->post('type');

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = UserConsume::find()
            ->where(['status' => 1])
            ->andWhere(['>', 'balance', 0])
            ->andWhere(['user_id' => $this->user->id]);

        // switch ($type) {
        //     case 'score':
        //         $query->andWhere(['>', 'score', 0]);
        //         break;
        //     case 'ticket':
        //         $query->andWhere(['>', 'ticket', 0]);
        //         break;
        //     case 'platform_ticket':
        //         $query->andWhere(['>', 'platform_ticket', 0]);
        //         break;
        //     case 'balance':
        //         $query->andWhere(['>', 'balance', 0]);
        //         break;
        // }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }
    public function actionConsume2()
    {
        // $type = $this->post('type');

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = UserConsume::find()
            ->where(['status' => 1])
            ->andWhere(['>', 'platform_ticket', 0])
            ->andWhere(['user_id' => $this->user->id]);

        // switch ($type) {
        //     case 'score':
        //         $query->andWhere(['>', 'score', 0]);
        //         break;
        //     case 'ticket':
        //         $query->andWhere(['>', 'ticket', 0]);
        //         break;
        //     case 'platform_ticket':
        //         $query->andWhere(['>', 'platform_ticket', 0]);
        //         break;
        //     case 'balance':
        //         $query->andWhere(['>', 'balance', 0]);
        //         break;
        // }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }
}